package org.kolonitsky.intuit.corbaejb.correcttime;

import org.kolonitsky.intuit.correcttime.CorrectTimeHome;
import org.kolonitsky.intuit.correcttime.CorrectTimeRemote;

import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import java.rmi.RemoteException;
import java.util.Date;
import java.util.Properties;

/**
 * @author Alex.Kolonitsky
 */

public class ClientApp {
    public static void main(String[] args) {
        try {
            Context jndiContext = createJBossContext();

            Object ref = jndiContext.lookup("CorrectTimeBean");

            CorrectTimeHome home = (CorrectTimeHome) PortableRemoteObject.narrow(ref, CorrectTimeHome.class);

            CorrectTimeRemote remote = home.create();
            Date localDate = new Date();
            Date remoteDate = remote.obtainCorrectTime();
            System.out.println("Remote time is  " + remoteDate.toString() +
                    "\nCurrent time is  " + localDate.toString());
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (NamingException e) {
            e.printStackTrace();
        } catch (CreateException e) {
            e.printStackTrace();  // TODO
        }
    }

    public static Context createJBossContext() throws NamingException {
        Properties p = new Properties();
        p.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
        p.put("java.naming.provider.url", "jnp://127.0.0.1:1099");
        p.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
        return new InitialContext(p);
    }
}
